﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace Ditto.Models
{
    public class ImageRepository
    {
        public void AddItemImage(Image image, Item item)
        {
            item.ItemPicture1 = "items/" + item.ItemID;
            string imagesDir = HttpContext.Current.Server.MapPath("~/Content/");

            image.Save(imagesDir + item.ItemPicture1 + ".jpg");
            image.SaveThumbnail(imagesDir + item.ItemPicture1 + "-small.jpg");
        }

        public void DeleteItemImage(Item item)
        {
            item.ItemPicture1 = null;
            string imagesDir = HttpContext.Current.Server.MapPath("~/Content/");

            File.Delete(imagesDir + "items/" + item.ItemID + ".jpg");
            File.Delete(imagesDir + "items/" + item.ItemID + "-small.jpg");
        }

        public void AddUserImage(Image image, User user)
        {
            user.UserPicture1 = "users/" + user.UserID;
            string imagesDir = HttpContext.Current.Server.MapPath("~/Content/");

            image.Save(imagesDir + user.UserPicture1 + ".jpg");
            image.SaveThumbnail(imagesDir + user.UserPicture1 + "-small.jpg");
        }

        public void DeleteUserImage(User user)
        {
            user.UserPicture1 = null;
            string imagesDir = HttpContext.Current.Server.MapPath("~/Content/");

            File.Delete(imagesDir + "users/" + user.UserID + ".jpg");
            File.Delete(imagesDir + "users/" + user.UserID + "-small.jpg");
        }
    }
}